package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import playerio.*;
	
	public class mainroom extends MovieClip 
	{
		static public var players:		Array 					= [];
		public var myName:				String 					= "Гость";
		public var players_list:		mainroom_playerslist 	= new mainroom_playerslist();
		public var nameField:			TextField 				= new TextField();
		public var nameField_title:		TextField 				= new TextField();
		public var connection:			Connection;
		public var myid:				int;
		
		public function mainroom(connection:Connection) 
		{
			this.connection = connection;
			connection.addDisconnectHandler(handleDisconnect);
			connection.addMessageHandler("hello", 		onLogin);
			connection.addMessageHandler("ChangeName", 	onChangeName);
			connection.addMessageHandler("UserJoined", 	onUserJoined);
			connection.addMessageHandler("UserLeft", 	onUserLeft);
			connection.addMessageHandler("*", 			handleMessages);
			myName += (int(Math.random() * 10000)).toString();
			connection.send("MyNameIs", myName);
			addEventListener(Event.ADDED_TO_STAGE, init);
			
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addChild(players_list);
			init_nameField();
			/*players.push([userid, username]);
			players_list.addItem(userid, username);
			players_list.refresh_players();*/
		}
		
		public function init_nameField():void 
		{
			nameField.type = TextFieldType.INPUT;
			nameField.x = 150;
			nameField.text = "123";
			nameField.background = true;
			nameField.backgroundColor = 0xEEEEEE;
			addChild(nameField);
			addChild(nameField_title);
			nameField_title.x = nameField.x;
			nameField_title.text = "Введите ваше имя";
			nameField_title.mouseEnabled = false;
			nameField.y += 20;
			nameField.addEventListener(Event.CHANGE, on_change_name);
		}
		
		private function on_change_name(e:Event):void 
		{
			myName = nameField.text;
			connection.send("MyNameIs", myName);
		}
		
		public function onChangeName(m:Message, userid:uint, username:String):void 
		{
			for each (var p:Array in players) 
			{
				if (p[0] == userid)
					p[1] = username;
			}
			trace("onChangeName");
			players_list.refresh_players();
			tracePlayers();
		}
		
		public function onUserLeft(m:Message, userid:uint):void 
		{
			trace("Пользователь", userid, " отключился");
			//tracePlayers();
			for (var i:int = 0; i < players.length; i++) 
			{
				if (players[i][0] == userid)
					delete players[i];
			}
			players_list.refresh_players();
			tracePlayers();
		}
		
		public function onUserJoined(m:Message, userid:uint):void 
		{
			var username:String = "Гость";
			trace("Пользователь [", userid, "] подключился");
			players.push([userid, username]);
			players_list.addItem(userid, username);
			players_list.refresh_players();
		}
		
		public function onLogin(m:Message, myid:int):void 
		{
			this.myid = myid;
		}
		
		public function handleDisconnect():void 
		{
			
		}
		
		private function handleMessages(m:Message):void
		{
			//trace("Recived the message", m);
		}
		
		private function tracePlayers():void 
		{
			var s:String = "";
			for each (var p:Array in players) 
			{
				s += p[1]+"("+p[0]+"),";
			}
			trace(s);
		}
	}

}